package com.formation.tocea.library.client.ui.main;

import java.util.Iterator;

import com.formation.tocea.library.client.listener.LoginLogoutListener;
import com.formation.tocea.library.client.listener.UpdateMainPanelContentListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Menu extends VerticalPanel {
	private Button connectButton;
	private Button updateOwnAccountButton;
	private Button manageBooksButton;
	private Button manageUsersButton;
	private Button manageRentsButton;
	
	public Menu() {
		init();
	}

	public void init() {
		setWidth("100%");
		setSpacing(3);
		
		connectButton = new Button("Se connecter");
		connectButton.setWidth("80%");
		
		connectButton.addClickHandler(new LoginLogoutListener());
		
		add(connectButton);
		
		setCellHorizontalAlignment(connectButton, HasHorizontalAlignment.ALIGN_CENTER);
		
		RootPanel.get("menu").add(this);
	}
	
	public void setConnectedMode() {
		connectButton.setText("Se déconnecter");
		connectButton.setEnabled(true);
		
		updateOwnAccountButton = new Button("Modifier vos données");
		manageBooksButton = new Button("Gestion des livres");
		manageUsersButton = new Button("Gestion des utilisateurs");
		manageRentsButton = new Button("Gestion des locations");
		
		updateOwnAccountButton.setWidth("80%");
		manageBooksButton.setWidth("80%");
		manageUsersButton.setWidth("80%");
		manageRentsButton.setWidth("80%");
		
		updateOwnAccountButton.addClickHandler(new UpdateMainPanelContentListener());
		manageBooksButton.addClickHandler(new UpdateMainPanelContentListener());
		
		add(updateOwnAccountButton);
		add(manageBooksButton);
		add(manageUsersButton);
		add(manageRentsButton);
		
		setCellHorizontalAlignment(updateOwnAccountButton, 	HasHorizontalAlignment.ALIGN_CENTER);
		setCellHorizontalAlignment(manageBooksButton, 		HasHorizontalAlignment.ALIGN_CENTER);
		setCellHorizontalAlignment(manageUsersButton, 		HasHorizontalAlignment.ALIGN_CENTER);
		setCellHorizontalAlignment(manageRentsButton, 		HasHorizontalAlignment.ALIGN_CENTER);		
	}
	
	public void setDisconnectedMode() {
		remove(updateOwnAccountButton);
		remove(manageBooksButton);
		remove(manageUsersButton);
		remove(manageRentsButton);
		
		//S'il existe des formulaires dans le menu, on les supprime
		Iterator<Widget> iterator = iterator(); 
		while (iterator.hasNext()) {
			Widget widget = iterator.next();
			if (widget instanceof Grid) {
				remove(widget);
			}
		}
	}

	public Button getConnectButton() {
		return connectButton;
	}

	public Button getUpdateOwnAccountButton() {
		return updateOwnAccountButton;
	}

	public Button getManageBooksButton() {
		return manageBooksButton;
	}

	public Button getManageUsersButton() {
		return manageUsersButton;
	}

	public Button getManageRentsButton() {
		return manageRentsButton;
	}
}
